<template>
  <div v-show="$route.name == 'goods-list'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row class="query">
        <el-col :span="24">
          <el-select v-model="goodsSearch.process" placeholder="请选择分类1" clearable popper-class="dropdownbox" filterable style="width: 150px;">
            <el-option v-for="item in processList" :key="item.value" :value="item.value" :label="item.label">
              <span style="float: left">{{ item.label }}</span>
            </el-option>
          </el-select>
          <el-select v-model="goodsSearch.category_id" placeholder="请选择分类2" clearable popper-class="dropdownbox" filterable style="width: 150px;margin-left: 5px;">
            <el-option v-for="item in categoryList" :key="item.id" :value="item.id" :label="item.category_name">
              <span style="float: left">{{ item.category_name }}</span>
            </el-option>
          </el-select>
          <el-input placeholder="请填写名称"
            v-model="goodsSearch.name"
            clearable style="width: 200px;margin-left: 5px;">
          </el-input>
          <el-button type="primary" @click="searchGoods" style="margin-left: 5px">查询</el-button>
          <el-button style="margin-left: 5px"  @click="reset">重置</el-button>
        </el-col>
      </el-row>
      <el-row v-if="groupid === 0">
        <el-col :span="24">
          <el-button type="success" @click="addGoods" style="margin-left: 5px">新增商品</el-button>
          <el-button type="success" @click="addCategory">新增分类</el-button>
          
        </el-col>
      </el-row>
      <el-table
        border
        v-loading="loading"
        :data="goodsData"
        style="margin-top: 10px;">
        <el-table-column label="商品ID" prop="id" min-width="80" align="center"></el-table-column>
        <el-table-column label="分类1" prop="process" width="80" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.process == 1 ? '成品' : '原料' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分类2" prop="category" min-width="130" align="center"></el-table-column>
        <el-table-column label="商品名称" prop="name" min-width="160" align="center"></el-table-column>
        <el-table-column label="销售名" prop="sale_name" min-width="150" align="center"></el-table-column>
        <el-table-column label="采购单位" prop="unit_in" width="85" align="center"></el-table-column>
        <el-table-column label="默认进货价/参考成本" prop="price_in" min-width="120" align="center" v-if="groupid === 0">
          <template slot-scope="scope">
            <span>{{ parseFloat(scope.row.price_in).toFixed(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="转换比例" prop="conversion" min-width="100" align="center">
          <template slot-scope="scope">
            <span>{{ parseFloat(scope.row.conversion).toFixed(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="入库单位" prop="unit" width="85" align="center"></el-table-column>
        <el-table-column label="售价" prop="price" min-width="100" align="center" v-if="groupid === 0">
          <template slot-scope="scope">
            <span>{{ parseFloat(scope.row.price).toFixed(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否可销售" prop="sellable" width="100" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.sellable == 1 ? '可销售' : '不可销售'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80" align="center" fixed="right" v-if="groupid === 0">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="clickEdit(scope.row.id)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.currPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.totalCount">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      goodsSearch: {
        process: '',
        category_id: '',
        name: ''
      },
      groupid: '',
      processList: [
        {
          value: '0',
          label: '原料'
        },
        {
          value: '1',
          label: '成品'
        }
      ],
      categoryList:[],
      loading: false,
      pager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      goodsData: []
    }
  },
  mounted() {
    this.getUserInfo()
    this.getCategoryList()
    this.getGoodsList()
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      const { data: result } = await this.$http.get('/user/' + localStorage.userid)
      if (result.status !== 200) return this.$message.error(result.msg)
      this.groupid = result.data.groupid * 1
    },
    // 获取分类
    async getCategoryList() {
      const { data: result } = await this.$http.get('/category')
      if (result.status !== 200) return this.$message.error(result.msg)
      this.categoryList = result.data
    },
    async getGoodsList() {
      this.loading = true
      const params = {
        page: this.pager.currPage,
        pageSize: this.pager.pageSize,
        process: this.goodsSearch.process,
        category_id: this.goodsSearch.category_id,
        name: this.goodsSearch.name
      }
      const { data: result } = await this.$http.get('/goods/list', {params: params})
      if (result.status !== 200) {
        this.loading = false
        this.goodsData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading = false
      this.goodsData = result.data
      this.pager = result.pager
    },
    clickEdit(id) {
      this.$router.push({
        name: 'goods-update',
        params: { id: id }
      })
    },
    handleSizeChange(pageSize) {
      this.pager.pageSize = pageSize
      this.getGoodsList()
    },
    handleCurrentChange(currentPage) {
      this.pager.currPage = currentPage
      this.getGoodsList()
    },
    searchGoods() {
      this.pager.currPage = 1
      this.getGoodsList()
    },
    reset() {
      this.goodsSearch = {
        process: '',
        category_id: '',
        name: ''
      },
      this.goodsData = []
      this.pager.currPage = 1
      this.getGoodsList()
    },
    addGoods() {
      this.$router.push({ 'name': 'goods-save' })
    },
    addCategory() {
      this.$prompt('请输入新分类名称', '新增分类', {
        confirmButtonText: '确认添加',
        cancelButtonText: '取消',
        inputPattern: /^.{1,10}$/,
        inputErrorMessage: '分类10个字符以内，不能为空'
      }).then(({ value }) => {
        this.addCat(value)
        return
      }).catch(() => {
        // this.categoryName = ''
      })
    },
    async addCat(value) {
      const { data: result } = await this.$http.post('/category', { categoryName: value })
      if (result.status !== 200) {
        return this.$message.error(result.msg)
      }
      this.getCategoryList()
      this.getGoodsList()
      return this.$message.success('添加分类成功')
    }
  }
}
</script>

<style lang="less">
  .query {
    margin-bottom: 20px;
  }
</style>